/**
 * Copyright(c) kyle.
 */
package org.example.jvm;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 掌控方法分配规则
 *
 * @author kyle
 * @version 1.00.00
 * @since [2020-12-03 22:40]
 * @updateUser [Kyle]
 * @updateRemark [说明本次修改内容]
 * @date [2020-12-03 22:40]
 */
public class GrandFatherTest {
    private static class GratherFather{
        void thinking(){
            System.out.println("i am grandfather");
        }
    }
    private static class Father extends GratherFather{
        @Override
        void thinking(){
            System.out.println("i am father");
        }
    }

    private static class Son extends Father{
        /**
         * 在此方法中调用祖类方法
         */
        @Override
        void thinking(){
            try {
                MethodType mt=MethodType.methodType(void.class);
                MethodHandle mh = MethodHandles.lookup().findSpecial(GratherFather.class, "thinking", mt, getClass());
                mh.invoke(this);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        (new Son()).thinking();
    }
}
